package user;

import java.sql.*;

import javax.servlet.http.HttpSession;


public class DisplayCheckoutPage
{
	//Display different list of food for different restaurant in the customer_page
	//Read a static variable from RestaurantQuery, not safe
	
	public static String GetCheckoutInfo(String username)
	{
		DBConnector db = DBConnector.getDBConnector();		
		//String Rest_ID = RestaurantQuery.getRestaurantSelected();		
		//HttpSession session = request.getSession();
		//Object usernameObject = session.getAttribute("username");
		//String CusID = LoginValidation.getUserId(usernameObject.toString());
		
		String CusID = LoginValidation.getUserId(username);	
		String AllOrderForms = "select form_ID from order_form where cus_ID = " + CusID + " order by form_time desc";
		System.out.println("Username here : " + CusID);
		db.prepareStatement(AllOrderForms);
		ResultSet AllOrderFormsResult = db.queryPstmt();
		try {
			if (AllOrderFormsResult != null)
			{
				
				AllOrderFormsResult.next();						
				
				String FormID = AllOrderFormsResult.getString(1);
				//System.out.println("Look at here : " + FormID); //test
				String statement = "select food_name, order_quantity, food_price, food.food_ID from food, contain, order_form" +
				" where contain.food_ID = food.food_ID and contain.form_ID = order_form.form_ID and " +
				"order_form.form_ID = " + FormID;
				//System.out.println("Statement here : " + statement);
				db.prepareStatement(statement);
				ResultSet CheckoutInfo = db.queryPstmt();
				return DisplayCheckoutItems(CheckoutInfo);		
			}
		} catch (SQLException e) {
			e.printStackTrace();	
			System.out.println("DisplayCheckoutPage Throw Exception");
		}
		//String statement = "select food_name, order_quantity, food_price, food.food_ID from food, contain where food.food_ID = contain.food_ID";
		//db.prepareStatement(statement);
		//ResultSet CheckoutInfo = db.query(statement);
		
		return null;
	}	
	
	private static String DisplayCheckoutItems(ResultSet CheckoutInfo)
	{
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (CheckoutInfo != null)
			{	
				//sBuf.append("");
				sBuf.append("<table width = 50% align = center border = 1><tr>");
				sBuf.append("<tr bgcolor=\"#666666\">");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Items</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Quantity</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Price</th>");
		        sBuf.append("</tr>");
				
				while (CheckoutInfo.next()){
		
					
					sBuf.append("<form action ='./checkout.jsp' method = 'get'>"+ 
							"<tr><td>"+
							CheckoutInfo.getString(1)+"</td><td>"+
							CheckoutInfo.getString(2)+"</td><td>"+
							CheckoutInfo.getString(3)+"</td><td>"+
							"<input type = 'hidden' name = 'food_ID' value = '"+CheckoutInfo.getString(4)+"'/>" + "</td><td>"+
							"<input type = 'submit' name = 'cancel" + CheckoutInfo.getString(4) + "'" + " value = 'cancel' />"+ "</td>"+
							"</tr></form>"
							);
					
				}
				sBuf.append("</table>");
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			//System.out.println("The connection is not established");
			return "";
		}finally{
		}	
	}
	
	/*public static String DeleteOrderForm()
	{
		DBConnector db = DBConnector.getDBConnector();
		String DeleteOrderForm = "delete from order_form where cus_ID"+ "= ? " +
				"and form_state = 'not cooked yet'";
		db.prepareStatement(DeleteOrderForm);
		db.bindParameter("1");
		db.updatePstmt();
		return "";
	}*/
	
////---------------------Below is used to display name, phone, address of a customer in the checkout page	
	
	public static String GetReceiverName(String username) {
		DBConnector db = DBConnector.getDBConnector();		
		
		String cid = LoginValidation.getUserId(username);
		String statement = "select cus_name from customer where cus_ID= " + cid; 		
		
		ResultSet ReceiverName = db.query(statement);
		
		return ReturnReceiverName(ReceiverName);		
	}
	

	private static String ReturnReceiverName(ResultSet ReceiverName)
	{
		StringBuffer sBuf = new StringBuffer();
		try {
			if (ReceiverName != null)
			{	
				while (ReceiverName.next()){

					//System.out.println(ReceiverName.getString(1));
					//return ReceiverName.getString(1);
					//<input type="text" name="clientCheckout_receiverName" value=<%=DisplayReceiverInfo.GetReceiverName()%> size="50"/>
					//sBuf.append("<input type=\"text\" name=\"clientCheckout_receiverName\" value=" + ReceiverName.getString(1) + "/>");
					sBuf.append("value='"+ReceiverName.getString(1)+"'");
				}
			}

			return sBuf.toString();
			
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}	
	}
	
	public static String GetReceiverCell(String username)
	{
		DBConnector db = DBConnector.getDBConnector();		
		
		String cid = LoginValidation.getUserId(username);
		String statement = "select cus_phone from customer where cus_ID= " + cid;		
		
		ResultSet ReceiverCell = db.query(statement);
		
		return ReturnReceiverCell(ReceiverCell);		
	}
	

	private static String ReturnReceiverCell(ResultSet ReceiverCell)
	{
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (ReceiverCell != null)
			{	
				while (ReceiverCell.next()){

					sBuf.append("value='"+ReceiverCell.getString(1)+"'");
				}
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}	
	}
	
	public static String GetReceiverAddress(String username){
		DBConnector db = DBConnector.getDBConnector();		
		String cid = LoginValidation.getUserId(username);
		String statement = "select cus_address from customer where cus_ID= " + cid; 		
		ResultSet ReceiverAddress = db.query(statement);
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (ReceiverAddress != null)
			{	
				while (ReceiverAddress.next()){

					sBuf.append("value='"+ReceiverAddress.getString(1)+"'");
				}
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}	
	}
	
	
}
